import tkinter as tk


root = tk.Tk()
root.title("Demo")
root.geometry("800x600")
app_frame = tk.Frame(root)
app_frame.pack(fill="both", expand=True)


# 标签
lvar = tk.StringVar()
lvar.set("hello world")
label = tk.Label(app_frame, textvariable=lvar, bg="green", font=("Arial", 12), width=15, height=2)
label.pack()

def print_selection():
    # print(var.get())
    lvar.set(var.get())


# 单选
var = tk.StringVar()
var.set("A")
radio1 = tk.Radiobutton(app_frame, text="A", variable=var, value="A",command=print_selection)
radio1.pack()
radio2 = tk.Radiobutton(app_frame, text="B", variable=var, value="B",command=print_selection)
radio2.pack()
radio3 = tk.Radiobutton(app_frame, text="C", variable=var, value="C",command=print_selection)
radio3.pack()



# 多选
selected_options = []  # 用于存储选中的选项

# 定义一个函数来处理多选事件
def multiple_selection():
    selected_options.clear()
    for option, var in option_vars.items():
        if var.get() == "selected":
            selected_options.append(option)
    print(selected_options)

# 为每个选项创建一个独立的变量
option_vars = {
    "A": tk.StringVar(),
    "B": tk.StringVar(),
    "C": tk.StringVar()
}

# 初始化变量的值为未选中
for var in option_vars.values():
    var.set("unselected")



# 复选框
check1 = tk.Checkbutton(app_frame, text="A", variable=option_vars["A"], onvalue="selected", offvalue="unselected", command=multiple_selection)
check1.pack()
check2 = tk.Checkbutton(app_frame, text="B", variable=option_vars["B"], onvalue="selected", offvalue="unselected", command=multiple_selection)
check2.pack()
check3 = tk.Checkbutton(app_frame, text="C", variable=option_vars["C"], onvalue="selected", offvalue="unselected", command=multiple_selection)
check3.pack()


root.mainloop()